<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');




class Fb extends CI_Controller {

	/*
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

	public function __construct()
    {

		parent::__construct();

		(!session_id()) ? session_start(): '';

		$this->load->helper("myuser_helper");



    }

	public function index()
	{

		echo "THIS APPLICATION IS CREATED BY ADVANZWEB.COM ";

	}

	public function support(){

		echo "supports";
	}

	public function policy(){

		echo "policy";
	}

	public function canvas(){

		echo "sample app page";

	}

	public function signup(){

		$facebook = new FacebookAccessLib();
		$user = $facebook->getUser();

		$fql = 'SELECT first_name, last_name, email,middle_name, username, birthday_date, about_me from user where uid = me() ';
		$ret_obj = $facebook->api(array(
						  'method' => 'fql.query',
						  'query' => $fql,
						));
		$fb_datas = $ret_obj[0];
		$fb_email  = $fb_datas['email'];


	}

	public function login(){
		$facebook = new FacebookAccessLib();
		$user_id = $facebook->getUser();
		if($user_id){
			$fql = 'SELECT first_name, last_name, email,middle_name, username, birthday_date, about_me from user where uid = '.$user_id;
			$ret_obj = $facebook->api(array(
				  'method' => 'fql.query',
				  'query' => $fql,
				));
			$fb_datas = $ret_obj[0];
			$fb_email  = $fb_datas['email'];
			if(!empty($fb_email)){ //email from facebook not empty
				//sytem user id
				$qry_check_user = $this->db->query("select email from mlme_users where email='$fb_email' limit 1");
				$found_user = $qry_check_user->num_rows();
				//Check if the email is existing on the website database or not.
				if($found_user){
					$found_userinfo = $qry_check_user->row();
					$_SESSION['email'] = $found_userinfo->email;
					$_SESSION['is_logged_in'] = 1;
					redirect("profile","refresh");
				}else{ // user_id = 0
					$first_name  = $fb_datas['first_name'];
					$last_name   = $fb_datas['last_name'];
					$middle_name = $fb_datas['middle_name'];
					$type       = 'free';
					$online      = 1;
					$status      = 1;
					$about       = $fb_datas['about_me'];
					$password    = Myuser_helper::randLetterNumber();
					$username    = $fb_datas['username'];
					$b_arr       = (!empty($fb_datas['birthday_date']))? explode("/",$fb_datas['birthday_date']):'';
					$birth_day   = (!empty($b_arr))? date("Y-m-d",mktime(0,0,0,$b_arr[0],$b_arr[1],$b_arr[2])):'';
					$picture     = "";
					 //user model loading
					 $this->load->model('users_model');
					$new_user = array(
						'firstname' => $first_name,
						'lastname' => $last_name,
						'middle' => $middle_name,
						'email' => $fb_email,
						'password' => sha1(md5($password)),
						'type' => 'free',
						'date_added' => date('Y-m-d h:i:s'),
						'date_updated' => date('Y-m-d h:i:s'),
						'status' => 1
					);
					$save = $this->db->insert('mlme_users',$new_user);
					if($save){
						$send_to = $fb_email;
						$headers  = 'MIME-Version: 1.0' . "\r\n";
						$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
						$headers .= "From: My Life My Eulogy <noreply@mylifemyeulogy.com>\r\n";
						$subject = "My Life My Eulogy - Account Details";
						$body  = 'Below is your account details:  <br/>Email:'.$fb_email.'<br/>Password:'.$password.'<br/><br/>';
						$url = base_url()."login/";
						$body .= 'Click the link below to log in to My Life My Eulogy<br/>';
						$body .= "<a href=".$url.">$url</a>";
						$body .='<br/><br/>Thank You';
						mail($send_to, $subject, $body,$headers);
						$_SESSION['email'] = $fb_email;
						$_SESSION['is_logged_in'] = 1;
						redirect("profile","refresh");
					}//end if save
				}//end if found user
			}else{
				$error = array();
				$error[] = 'Unable to get your information from facebook. Please try to check your profile privacy settings.';
				$_SESSION['post_data']['email'] = '';
				$_SESSION['error'] = $error;
				redirect("login","refresh");
			}//end if not empty fb email
		}else redirect("home","refresh");
	
	}


	public function terms(){

	echo "terms";
	}

	public function logout(){

		session_destroy();

		redirect("fb/login","refresh");

	}




}
